package org.devgframwork.datasync.admin.support.init.schedule;

import org.devgframwork.datasync.core.entity.ScheduleConfig;
import org.devgframwork.datasync.core.support.service.ScheduleConfigService;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Date;

public class ScheduleTrigger implements Trigger {

    private ScheduleConfig scheduleConfig;

    private ScheduleConfigService scheduleConfigService;

    public ScheduleTrigger(ScheduleConfig scheduleConfig, ScheduleConfigService scheduleConfigService) {
        this.scheduleConfig = scheduleConfig;
        this.scheduleConfigService = scheduleConfigService;
    }

    @Override
    public Date nextExecutionTime(TriggerContext triggerContext) {
        ScheduleConfig cornConfig = scheduleConfigService.getById(scheduleConfig.getScheduleConfigId());
        CronTrigger trigger = new CronTrigger(cornConfig.getCron());
        Date nextExec = trigger.nextExecutionTime(triggerContext);
        return nextExec;
    }
}
